package com.xysoft.framework.mvc;

import java.util.Map;

import org.apache.commons.lang3.builder.Builder;
import org.springframework.web.servlet.ModelAndView;

public class ModelAndViewBuilder implements Builder<ModelAndView> {
	
	protected ModelAndView modelAndView;
	
	public ModelAndViewBuilder() {
		modelAndView = newModelAndView();
	}
	
	protected ModelAndView newModelAndView() {
		return new ModelAndView();
	}
	
	public ModelAndViewBuilder freemarker(String location) {
		modelAndView.setViewName(ViewConstants.FREEMARKER_VIEW_PREFIX + location);
		return this;
	}
	
	public ModelAndViewBuilder redirect(String location) {
		modelAndView.setViewName(ViewConstants.REDIRECT_VIEW_PREFIX + location);
		return this;
	}
	
	public ModelAndViewBuilder jackson() {
		modelAndView.setViewName(ViewConstants.JSON_VIEW_PREFIX);
		return this;
	}
	
	public ModelAndViewBuilder jackson(Object object) {
		modelAndView.setViewName(ViewConstants.JSON_VIEW_PREFIX);
		addObject(ViewConstants.JSON_MODEL_KEY, object);
		return this;
	}
	
	public ModelAndViewBuilder html() {
		modelAndView.setViewName(ViewConstants.HTML_VIEW_PREFIX);
		return this;
	}
	
	public ModelAndViewBuilder addAllObjects(Map<String, ?> modelMap) {
		modelAndView.addAllObjects(modelMap);
		return this;
	}
	
	public ModelAndViewBuilder addObject(String attributeName, Object attributeValue) {
		modelAndView.addObject(attributeName, attributeValue);
		return this;
	}
	
	public ModelAndViewBuilder addObject(Object attributeValue) {
		modelAndView.addObject(attributeValue);
		return this;
	}
	
	@Override
	public ModelAndView build() {
		return modelAndView;
	}
}
